package com.xmu.edu.tisp.service.basedata;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xmu.edu.tisp.dao.Service;
import com.xmu.edu.tisp.dao.TISP_dbHelper;

public class CM_LEAF_POSITION_service implements Service{
	

	public static final String TABLE_NAME = "CM_LEAF_POSITION";
	public static final String PRIMARY_KEY="LEAF_POSITION_UUID";
	
	private SQLiteDatabase myDB;
	private SQLiteOpenHelper dbHelper;


	public CM_LEAF_POSITION_service(Context cxt) {
		dbHelper=new TISP_dbHelper(cxt, "TISP");
	}

	public SQLiteDatabase getdb()
	{
		if(myDB==null)
		{
			myDB=dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}
	
	@Override
	public void g_updata(ContentValues values) {
		getdb().update(TABLE_NAME, values, PRIMARY_KEY+"=?", new String[]{(String) values.get(PRIMARY_KEY)});
		System.out.println(TABLE_NAME+"have updataed!");
		
	}

	@Override
	public void g_insert(ContentValues values) {
		getdb().insert(TABLE_NAME, null, values);
		
		System.out.println(TABLE_NAME+"have inserted!");
	}

	@Override
	public void g_delete(String[] id) {
		getdb().delete(TABLE_NAME, PRIMARY_KEY+"=?", id);
		System.out.println(TABLE_NAME+"have deleted!");
		
	}


	@Override
	public List<String> g_getIDs() {
		Cursor c=getdb().query(TABLE_NAME, new String[]{PRIMARY_KEY},null, null, null, null, null);
		List<String> result=new ArrayList<String>();
		while(c.moveToNext())
		{
			String id=c.getString(c.getColumnIndex(PRIMARY_KEY));
			result.add(id);
		}
		return result;
	}
	public void close()
	{
		getdb().close();
		System.out.println("db closed");
	}
	
	
	//---------------------------------------------------------------
	//s  <---   s
	public String getPosition_name_from_cd(String cd)
	{
		String po_name="";
		String sql;
		sql="select LEAF_POSITION_NAME as a from "+TABLE_NAME+" where LEAF_POSITION_CD is '"+cd+"';";
		Cursor cursor = getdb().rawQuery(sql, null);
		while(cursor.moveToNext()) {
			po_name=(cursor.getString(cursor.getColumnIndex("a")));	
		}		
		cursor.close();
		return po_name;
	}
	public Vector getPosition_cd()
	{
		String sql;
		Vector v=new Vector<String>();
		//sql="select FARMER_ID from TF_FARMER_FINAL where TECHNICIAN_ID is '"+service_ids.get(i)+"'";
		sql="select LEAF_POSITION_CD  as a from "+TABLE_NAME+";";
		Cursor cursor = getdb().rawQuery(sql, null);
		while(cursor.moveToNext()) {
			v.add(cursor.getString(cursor.getColumnIndex("a")));	
		}		
		cursor.close();
		return v;
	}
	public Vector getPosition_name()
	{
		String sql;
		Vector v=new Vector<String>();
		sql="select LEAF_POSITION_NAME as a from "+TABLE_NAME+";";
		Cursor cursor = getdb().rawQuery(sql, null);
		while(cursor.moveToNext()) {
			v.add(cursor.getString(cursor.getColumnIndex("a")));	
		}		
		cursor.close();
		return v;
	}
}
